home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / pctchnqs / 1991 / number4 / smemcpy.asm < prev    next >
Assembly Source File  |  1991-08-02  |  1KB  |  44 lines

  1. ; smemcpy.asm: Smart memory copy routine
  2. ; Runs in protected mode; avoids protection faults by validating
  3. ; the pointers given before attempting the copy.
  4. ; Copyright (C) 1991 by Nicholas Wilt.    All rights reserved.
  5.  
  6. .MODEL    LARGE
  7.  
  8. .286P
  9.  
  10. .CODE
  11.  
  12. ; void smemcpy(void *dest, void *src, unsigned numbytes);
  13.  
  14.     PUBLIC    _smemcpy
  15.  
  16. _smemcpy   PROC
  17.     push    bp        ; Establish stack frame
  18.     mov    bp,sp        ;
  19.     push    ds        ; Save registers
  20.     push    si        ;
  21.     push    di        ;
  22.  
  23.     mov    ax,[bp+8]    ; Get selector of dest pointer
  24.     verw    ax        ; Can we write to it?
  25.     jnz    DoneCopy    ; Jump if no
  26.     mov    es,ax        ; Otherwise put selector in ES
  27.     mov    si,[bp+6]    ; Get offset portion of pointer
  28.     mov    ax,[bp+12]    ; Get selector of src pointer
  29.     verr    ax        ; Can we read from it?
  30.     jnz    DoneCopy    ; Jump if no
  31.     mov    ds,ax        ; Other
  32.     mov    di,[bp+10]    ; Get offset portion of pointer
  33.     mov    cx,[bp+14]    ; Get count
  34.     rep    movsb        ; Do the copy (slowly, I know)
  35. DoneCopy:
  36.     pop    di        ; Restore registers
  37.     pop    si        ;
  38.     pop    ds        ;
  39.     pop    bp        ; Restore stack frame
  40.     ret            ; Return
  41. _smemcpy   ENDP
  42.  
  43.     END
  44.